package com.jinbei.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;

/**
 * @author zflu
 * jsm工厂初始化
 * Created by lucky on 2018/8/7.
 */
@Configuration
@EnableJms
public class JmsFactoryConfig {

    @Value("${activemq.ClientId}")
    private String ClientId;

    @Bean
    public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        //订阅发布
        factory.setPubSubDomain(true);
        //消息持久化
        factory.setSubscriptionDurable(true);
        //链接超时
        factory.setReceiveTimeout(1000L);
        //接收者id
        factory.setClientId(ClientId);
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

    @Bean
    public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        //队列
        factory.setPubSubDomain(false);
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

}
